home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / ftp / archie132.lha / archie-1.3.2 / src / archie.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-10  |  7.3 KB  |  301 lines

  1. /*
  2.  * Copyright (c) 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <copyright.h>.
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <clib/exec_protos.h>
  10.  
  11. #include <copyright.h>
  12.  
  13. /*
  14.  * Archie client using the Prospero protocol.
  15.  *
  16.  * Suggestions and improvements to Brendan Kehoe (brendan@cs.widener.edu).
  17.  */
  18.  
  19. #include <stdio.h>
  20. #if defined(OS2)
  21. # include <pctcp.h>
  22. #endif
  23. #ifdef MSDOS
  24. # include <string.h>
  25. # include <stdlib.h>
  26. # ifdef CUTCP
  27. #  include <msdos/cutcp.h>
  28. #  include <msdos/hostform.h>
  29. /* The default stack size for a BC program is 4k; jack it up to 16 and add the
  30.    Check for Stack Overflow option to the compiler.  */
  31. extern unsigned _stklen = 16 * 1024;
  32. # endif
  33. #endif
  34.  
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <pfs.h>
  38. #include <rdgram.h>
  39. #include <archie.h>
  40. #include <pmachine.h>
  41.  
  42. int        listflag = 0;
  43. int        sortflag = 0;   /* 1 = by date                    */
  44. char        *progname;
  45. #ifdef DEBUG
  46. extern int    pfs_debug;
  47. #endif
  48. extern int    rdgram_priority;
  49.  
  50. /* To keep the code clean.. */
  51. #ifdef VMS
  52. # define NFLAG    "\"N\""
  53. # define LFLAG    "\"L\""
  54. # define A_EXIT    SS$_NORMAL
  55. #else
  56. # define NFLAG    "N"
  57. # define LFLAG    "L"
  58. # define A_EXIT    1
  59. #endif
  60. #ifdef CUTCP
  61. # define HFLAG    "[H config.tel]]"
  62. #else
  63. # define HFLAG    "]"
  64. #endif
  65.  
  66. main(argc,argv)
  67.     int        argc;
  68.     char    *argv[];
  69.     {
  70.  
  71.     struct Library *usergroupBase;
  72.  
  73.     char        *cur_arg;
  74.     char        qtype = '=';    /* Default to exact string match  */
  75.     char        etype = '=';    /* Type if only -e is specified   */
  76.     int        eflag = 0;    /* Exact flag specified          */
  77.     int        max_hits = MAX_HITS;
  78.     int        offset = 0;
  79.     int        exitflag = 0;    /* Display release identifier     */
  80.     int        tmp;
  81.     char        *host = ARCHIE_HOST;
  82.     char        *p;
  83.     static char *archies[] = { ARCHIES };
  84. #ifdef CUTCP
  85.     int        hostset = 0;
  86. #endif
  87.     char        *getenv();
  88.  
  89. usergroupBase = OpenLibrary("usergroup.library",0);
  90.  
  91.     progname = *argv;
  92.     argc--; argv++;
  93.  
  94. #ifdef    CUTCP
  95.     if(getenv("CONFIGTEL"))
  96.         if(Shostfile(getenv("CONFIGTEL")) < 0) {
  97.             fprintf(stderr,"Error, couldn't open configtel file %s\n",
  98.                 getenv("CONFIGTEL"));
  99.             exit(1);
  100.         }
  101. #endif
  102.  
  103.     if ((p = getenv("ARCHIE_HOST")) != (char *)NULL)
  104.       host = p;
  105.         
  106.     while (argc > 0 && **argv == '-') {
  107.         cur_arg = argv[0]+1;
  108.  
  109.         /* If a - by itself, or --, then no more arguments */
  110.         if(!*cur_arg || ((*cur_arg == '-') && (!*(cur_arg+1)))) {
  111.             argc--, argv++;
  112.         goto scandone;
  113.         }
  114.  
  115.         while (*cur_arg) {
  116.         switch (*cur_arg++) {
  117. #ifdef DEBUG        
  118.         case 'D':  /* Debug level */
  119.             pfs_debug = 1; /* Default debug level */
  120.             if(*cur_arg && index("0123456789",*cur_arg)) {
  121.             sscanf(cur_arg,"%d",&pfs_debug);
  122.             cur_arg += strspn(cur_arg,"0123456789");
  123.             }
  124.             else if(argc > 2) {
  125.                 tmp = sscanf(argv[1],"%d",&pfs_debug);
  126.             if (tmp == 1) {argc--;argv++;}
  127.             }
  128.             break;
  129. #endif
  130. #ifdef    CUTCP
  131.         case 'H' :
  132.             if(Shostfile(argv[1]) < 0) {
  133.                 fprintf(stderr,"Error, couldn't open configtel file %s\n",argv[1]);
  134.                 exit(1);
  135.             }
  136.              argc--;argv++;
  137.             break;
  138. #endif
  139. #ifndef XARCHIE
  140.         case 'L':
  141.             printf("Known archie servers:\n");
  142.             for (tmp = 0; tmp < NARCHIES; tmp++)
  143.             printf("\t%s\n", archies[tmp]);
  144.             printf(" * %s is the default Archie server.\n", ARCHIE_HOST);
  145.             printf(" * For the most up-to-date list, write to an Archie server and give it\n   the command `servers'.\n");
  146.             exitflag = 1;
  147.             break;
  148. #endif
  149.  
  150.         case 'N':  /* Priority (nice) */
  151.             rdgram_priority = RDGRAM_MAX_PRI; /* Use this if no # */
  152.             if(*cur_arg && index("-0123456789",*cur_arg)) {
  153.             sscanf(cur_arg,"%d",&rdgram_priority);
  154.             cur_arg += strspn(cur_arg,"-0123456789");
  155.             }
  156.             else if(argc > 2) {
  157.                 tmp = sscanf(argv[1],"%d",&rdgram_priority);
  158.             if (tmp == 1) {argc--;argv++;}
  159.             }
  160.             if(rdgram_priority > RDGRAM_MAX_SPRI) 
  161.             rdgram_priority = RDGRAM_MAX_PRI;
  162.             if(rdgram_priority < RDGRAM_MIN_PRI) 
  163.             rdgram_priority = RDGRAM_MIN_PRI;
  164.               break;
  165.  
  166.         case 'c':  /* substring (case sensitive) */
  167.             qtype = 'C';
  168.             etype = 'c';
  169.             break;
  170.  
  171.         case 'e':  /* Exact match */
  172.             /* If -e specified by itself, then we use the  */
  173.             /* default value of etype which must be '='    */
  174.             eflag++;
  175.             break;
  176.  
  177.         case 'h':  /* Host */
  178.             host = argv[1];
  179. #ifdef CUTCP
  180.             hostset++;
  181. #endif
  182.             argc--; argv++;
  183.             break;
  184.  
  185.         case 'l':  /* List one match per line */
  186.             listflag++;
  187.             break;
  188.  
  189.         case '0': case '1': case '2': case '3': case '4':
  190.         case '5': case '6': case '7': case '8': case '9':
  191.             cur_arg--;
  192.         case 'm':  /* Max hits */
  193.             max_hits = -1;  
  194.             if(*cur_arg && index("0123456789",*cur_arg)) {
  195.             sscanf(cur_arg,"%d",&max_hits);
  196.             cur_arg += strspn(cur_arg,"0123456789");
  197.             }
  198.             else if(argc > 1) {
  199.                 tmp = sscanf(argv[1],"%d",&max_hits);
  200.             if (tmp == 1) {argc--;argv++;}
  201.             }
  202.             if (max_hits < 1) {
  203.             fprintf(stderr, "%s: -m option requires a value for max hits (>= 1)\n",
  204.                 progname);
  205.             exit(A_EXIT);
  206.             }
  207.             break;
  208.  
  209.         case 'o':  /* Offset */
  210.             if(argc > 1) {
  211.               tmp = sscanf(argv[1],"%d",&offset);
  212.               if (tmp != 1)
  213.             argc = -1;
  214.               else {
  215.             argc--; argv++;
  216.               }
  217.             }
  218.             break;
  219.  
  220.         case 'r':  /* Regular expression search */
  221.             qtype = 'R';
  222.             etype = 'r';
  223.             break;
  224.  
  225.         case 's':  /* substring (case insensitive) */
  226.             qtype = 'S';
  227.             etype = 's';
  228.             break;
  229.  
  230.         case 't':  /* Sort inverted by date */
  231.             sortflag = 1;
  232.             break;
  233.  
  234.         case 'v':  /* Display version */
  235.             fprintf(stderr,
  236.             "Client version %s based upon Prospero version %s\n",
  237.                 CLIENT_VERSION, PFS_RELEASE);
  238.             exitflag++;
  239.             break;
  240.  
  241.         default:
  242.             fprintf(stderr,"Usage: %s [-[cers][l][t][m#][h host][%s][%s#]%s string\n", progname, LFLAG, NFLAG, HFLAG);
  243.             exit(A_EXIT);
  244.         }
  245.         }
  246.         argc--; argv++;
  247.     }
  248.  
  249.       scandone:
  250.  
  251.     if (eflag) qtype = etype;
  252.  
  253.     if ((argc != 1) && exitflag) exit(0);
  254.  
  255.     if (argc != 1) {
  256.         fprintf(stderr, "Usage: %s [-[cers][l][t][m#][h host][%s][%s#]%s string\n", progname, LFLAG, NFLAG, HFLAG);
  257.         fprintf(stderr,"       -c : case sensitive substring search\n");
  258.         fprintf(stderr,"       -e : exact string match (default)\n");
  259.         fprintf(stderr,"       -r : regular expression search\n");
  260.         fprintf(stderr,"       -s : case insensitive substring search\n");
  261.         fprintf(stderr,"       -l : list one match per line\n");
  262.         fprintf(stderr,"       -t : sort inverted by date\n");
  263.         fprintf(stderr,"      -m# : specifies maximum number of hits to return (default %d)\n", max_hits);
  264.         fprintf(stderr,"  -h host : specifies server host\n");
  265.         fprintf(stderr,"       -%s : list known servers and current default\n", LFLAG);
  266.         fprintf(stderr,"      -%s# : specifies query niceness level (0-35765)\n", NFLAG);
  267. #ifdef CUTCP
  268.         fprintf(stderr,"-H config.tel: specify location of config.tel file\n");
  269. #endif
  270.         exit(A_EXIT);
  271.     }
  272.  
  273. #ifdef    CUTCP
  274.     if(argc = Snetinit()) {
  275.                fprintf(stderr,"Error %d from SNetinit (bad or missing config.tel ?)\n",argc);
  276.         if(argc == -2)
  277.                    netshut();    /* rarp lookup failure */
  278.         exit(1);
  279.     }
  280.     if(!hostset) {        /* if no host on command line, look in config.tel file
  281.                           for name=archie */
  282.         struct machinfo *mp;
  283.  
  284.          mp = Shostlook("archie");
  285.         if(mp) {
  286.                    host = mp->hname ? mp->hname : mp->sname;
  287.         }
  288.        }
  289. #endif
  290.  
  291.     procquery(host, argv[0], max_hits, offset, qtype, sortflag, listflag);
  292.  
  293. #ifdef CUTCP
  294.     netshut();
  295. #endif
  296.  
  297. CloseLibrary(usergroupBase);
  298.  
  299.     exit(0);
  300.     }
  301.